package com.zealcomm.ivcscustomer.pdfview;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.github.barteksc.pdfviewer.listener.OnLoadCompleteListener;
import com.github.barteksc.pdfviewer.listener.OnPageChangeListener;
import com.github.barteksc.pdfviewer.listener.OnPageErrorListener;
import com.github.barteksc.pdfviewer.scroll.DefaultScrollHandle;
import com.zealcomm.commonui.base.BaseCallback;
import com.zealcomm.commonui.base.ViewBindingFragment;
import com.zealcomm.ivcscustomer.R;
import com.zealcomm.ivcscustomer.databinding.FragmentPdfviewBinding;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class PdfviewFragment extends ViewBindingFragment<FragmentPdfviewBinding> implements OnPageChangeListener, OnLoadCompleteListener,
        OnPageErrorListener {

    InputStream inputStream;

    public static PdfviewFragment getInstance(String url, BaseCallback callback) {
        PdfviewFragment fragment = new PdfviewFragment();
        Bundle args = new Bundle();
        args.putString("url", url);
        fragment.setArguments(args);
        fragment.setBaseCallback(callback);
        return fragment;
    }

    protected InputStream getInputStreamFromURL(String sUrl) {
        InputStream inputStream = null;
        try {
            URL url = new URL(sUrl);
            HttpURLConnection urlConnection = (HttpURLConnection)
                    url.openConnection();
            urlConnection.setRequestMethod("GET");
            if (urlConnection.getResponseCode() == 200) {
                inputStream = new
                        BufferedInputStream(urlConnection.getInputStream());

            }
        } catch (IOException e) {

            return null;
        }
        return inputStream;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        PdfviewFragment that = this;
        new Thread() {
            @Override
            public void run() {
                inputStream = getInputStreamFromURL(getArguments().getString("url"));
                that.getHoldingActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        loadPdf();
                    }
                });
            }
        }.start();
        setListeners();
    }

    private void setListeners() {
        getBinding().actionBar.back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                removeFragment();
            }
        });
    }

    protected void loadPdf() {
        getBinding().pdfView.fromStream(inputStream)
                .defaultPage(0)
                .onPageChange(this)
                .enableAnnotationRendering(true)
                .onLoad(this)
                .scrollHandle(new DefaultScrollHandle(getHoldingActivity()))
                .spacing(10)
                .onPageError(this)
                .load();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_pdfview;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void onPageChanged(int page, int pageCount) {

    }

    @Override
    public void loadComplete(int nbPages) {

    }

    @Override
    public void onPageError(int page, Throwable t) {
    }

}
